/*
 * Copyright (c) 2025 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */
#pragma once
#include "internal/bus/bus_base/bus.h"

namespace dev {
class BusLock {
public:
    // 使用示例：
    // {
    //     BusLock lock(bus_ptr);  // 构造函数自动调用bus_lock()
    //     // 在这里执行需要总线锁保护的操作
    //     // ...
    // } // 作用域结束时，析构函数自动调用bus_unlock()

    // 构造函数，调用bus_lock
    explicit BusLock(bus_base& bus) : m_bus(bus) {
        m_bus.bus_lock();
    }

    // 析构函数，调用bus_unlock，不抛出异常
    ~BusLock() noexcept {
        try {
            m_bus.bus_unlock();
        } catch (...) {
        }
    }

private:
    bus_base& m_bus;
};
} // namespace dev